<?php

namespace Admin\Controller;

class LinkController extends CommonController
{
    var $link;

    function __construct()
    {
        parent::__construct();
        $this->link = D('Link');
    }

    function index()
    {

        $links = $this->link->order('sort_order')->select();

        $this->assign('links', $links);


        $this->display();

    }

    function add()
    {
        if (IS_POST) {
            $b = $this->link->create();
            if (!$b) {
                $this->error($this->link->getError());
            } else {
                $this->link->add();
                $this->success('添加成功');
            }
        }
    }

    function delete()
    {
        $id = I('get.id');
        $this->link->delete($id);
        $this->success('成功删除当条数据');

    }

    function edit()
    {
        if (IS_POST) {
            $this->link->create();
            $this->link->save();
            $this->success('成功修改');
        }

    }

    function delete_all()
    {
        $data['id'] = array('in', I('post.id'));
        $this->link->where($data)->delete();
        $this->success('删除成功');

    }

    function sort_order()
    {
        $ids = I('post.id');
        $sort_orders = I('post.sort_order');

        foreach ($ids as $key => $val) {

            $this->link->where("id=$val")->setField('sort_order', $sort_orders[$key]);
        }
        $this->success('排序成功');
    }

}